package com.yxw.yx_hotel.utils;

import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 登陆人员工具类
 * @author liuxin
 * @date 2023.09.12
 */
@Component
public class UserUtil {

    private final HttpServletRequest request;
    @Autowired
    public UserUtil(HttpServletRequest request){
        this.request = request;
    }

    public UserTokenVo getLoginUserVo(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){           
            return JwtUtil.parseToken(token);
        }
        else
        {
            /*UserTokenVo userTokenVo = new UserTokenVo();
            userTokenVo.setUserId(1L);
            userTokenVo.setNickName("lx");
            userTokenVo.setTenantId(11111L);
            return userTokenVo;*/
            throw new BaseException("未获取到操作人信息");
        }
    }
    public Long getLoginUserId(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            if(null != yxUserTokenVo)
            return yxUserTokenVo.getUserId();
            //return yxUserTokenVo == null ? null : yxUserTokenVo.getUserId();
        }
        throw new BaseException("未获取到操作人信息");
    }

    public String getLoginName(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            //return yxUserTokenVo == null ? null : yxUserTokenVo.getNickName();
            if(null != yxUserTokenVo)
            return yxUserTokenVo.getNickName();
        }
       throw new BaseException("未获取到操作人信息");
    }

    public String getLoginMobilePhone(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            if(null != yxUserTokenVo)
            return yxUserTokenVo.getMobilePhone();
        }
        throw new BaseException("未获取到操作人信息");
    }

    public String getLoginUserName(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            if(null != yxUserTokenVo)
            return yxUserTokenVo.getUserName();
        }
        throw new BaseException("未获取到操作人信息");
    }

    /**
     * 获取租户ID
     * @return
     */
    public Long getLoginTenantId(){
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            UserTokenVo yxUserTokenVo = JwtUtil.parseToken(token);
            if(null != yxUserTokenVo)
            return yxUserTokenVo.getTenantId();
        }
        throw new BaseException("未获取到操作人信息");
    }
}
